using System.Linq;
using Jurassic.Library;

namespace Kiwi.Node.Modules.Builtin
{
    public class BuiltinModuleObject: ObjectInstance
    {
        public IApplication Application { get; private set; }

        public BuiltinModuleObject(IApplication application): base(application.Engine)
        {
            Application = application;
            PopulateFunctions();
        }

        public BuiltinModuleObject(IApplication application, FunctionInstance inherits)
            : base(application.Engine.Object.Construct())
        {
            Application = application;
            Inherits(inherits);
            PopulateFunctions();
        }

        private void Inherits(FunctionInstance constructor)
        {
            var constructorPrototype = constructor["prototype"] as ObjectInstance;
            if (!HasProperty("prototype"))
            {
                this["prototype"] = Prototype;
            }
            var proto = Prototype;


            foreach(var property in constructorPrototype.Properties.Where(p => p.IsEnumerable))
            {
                proto[property.Name] = property.Value;
            }
        }
    }
}